package com.example.importandexport;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

//循环向数据库插入数据  for  sqlyang
public class ForCirculation {
    public static void main(String[] args) {
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 声明数据库view的URL
            String url = "jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false";
            // 数据库用户名
            String user = "root";
            // 数据库密码
            String password = "root";
            // 建立数据库连接，获得连接对象conn
            Connection conn = DriverManager.getConnection(url, user, password);
            String sql = "insert into student (sno,sname,sdept,ssex,sage,phone) values(?,?,?,?,?,?)"; // 生成一条sql语句
            // 创建一个Statment对象
            PreparedStatement ps = conn.prepareStatement(sql);

            long phone = 10000000000l;
            int age = 0;

            Long a1 = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {
                // 为sql语句中第一个问号赋值
                ps.setString(1, "3");
                // 为sql语句中第二个问号赋值
                ps.setString(2, "循环");
                // 为sql语句中第三个问号赋值
                ps.setString(3, "女");
                // 为sql语句中第四个问号赋值
                ps.setString(4, "hh");
                age = age+1;
                if (age >= 100 ){
                    age = age - 99;
                }
                ps.setInt(5, age);
                phone = phone+1;
                ps.setLong(6, phone);


                // 执行sql语句
                ps.executeUpdate();
            }
            Long a2 = System.currentTimeMillis();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
            System.out.println(formatter.format( new Date(a2)));


            System.out.println("所用时间"+ (a2-a1));

            // 关闭数据库连接对象
            conn.close();
            System.out.println("jjk插入完毕！！！");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
